﻿/// <reference path="class.js" />
/// <reference path="cryptojs-sha1.js" />
/// <reference path="http-requester.js" />
var HoodNews = HoodNews || {};

HoodNews.data = (function () {
    var DataPersister = Class.create({
        init: function (serviceRootUrl) {
            this.serviceRootUrl = serviceRootUrl;

            this.user = new UserPersister(serviceRootUrl + "user/");
            this.article = new ArticlePersister(serviceRootUrl + "articles", this.user);
            this.comment = new CommentPersister(serviceRootUrl + "comments", this.user);
            this.vote = new VotePersister(serviceRootUrl + "votes", this.user);
        }
    });

    var UserPersister = Class.create({
        init: function (serviceUrl) {
            this.serviceUrl = serviceUrl;
        },
        _getSessionId: function () {
            return localStorage.getItem("sessionId");
        },

        _getNickname: function () {
            return localStorage.getItem("nickname");
        },

        _setSessionId: function (value) {
            localStorage.setItem("sessionId", value);
        },

        _setNickname: function (value) {
            this.nickname = value;
            localStorage.setItem("nickname", value);
        },

        _clearSessionId: function () {
            localStorage.removeItem("sessionId");
        },

        _clearNickname: function () {
            localStorage.removeItem("nickname");
        },

        isLoggedin: function () {
            var isLoggedin = this._getSessionId() != null;

            return isLoggedin;
        },

        register: function (userName, nickName, password) {
            var self = this;

            return HttpRequester.postJson(this.serviceUrl + "register", {
                username: userName,
                nickname: nickName,
                authCode: CryptoJS.SHA1(userName + password).toString()
            }).then(function (result) {
                self._setSessionKey(result.sessionKey);
                self._setNickname(result.nickname);
                self._setUsername(userName);
            });
        },

        login: function (userName, password) {
            var self = this;
            return HttpRequester.postJson(this.serviceUrl + "login", {
                username: userName,
                authCode: CryptoJS.SHA1(userName + password).toString()
            }).then(function (result) {
                self._setSessionKey(result.sessionKey);
                self._setNickname(result.nickname);
                self._setUsername(userName);
            });
        },

        logout: function () {
            var self = this;

            return HttpRequester.getJson(this.serviceUrl + "logout/" + this._get())
            .then(function () {
                self._clearUsername();
                self._clearNickname();
                self._clearSessionKey();
            });
        },

        scores: function () {
            var self = this;

            return HttpRequester.getJson(this.serviceUrl + "scores/" + this._getSessionKey())
            .then(function (result) {
                return result;
            });
        }
    });

    var ArticlePersister = Class.create({
        init: function (serviceUrl, userPersister) {
            this.serviceUrl = serviceUrl;
            this.user = userPersister;
        },
        getAll: function (count, myArticles) {
            var url = this.serviceUrl + "?sessionId=" + this.user._getSessionId() + "&count=" + count + "&myArticles=";
            if (myArticles) {
                url += "true";
            } else {
                url += "false";
            }

            return HttpRequester.getJson(url);
        },
        get: function (id) {
            var url = this.serviceUrl + "/" + id + "?sessionId=" + this.user._getSessionId();

            return HttpRequester.getJson(url);
        },
        create: function (title, content, images) {
            var url = this.serviceUrl + "?sessionId=" + this.user._getSessionId();
            var data = {
                title: title,
                content: content,
                images: images
            };

            return HttpRequester.postJson(url, data);
        },
        update: function (id, title, content, images) {
            var url = this.serviceUrl + "/" + id + "?sessionId=" + this.user._getSessionId();
            var data = {
                id:id,
                title: title,
                content: content,
                images: images,
            };

            return HttpRequester.putJson(url, data);
        },
        remove: function (id) {
            var url = this.serviceUrl + "/" + id + "?sessionId=" + this.user._getSessionId();

            return HttpRequester.deleteJson(url);
        }
    });

    var CommentPersister = Class.create({
        init: function (serviceUrl, userPersister) {
            this.serviceUrl = serviceUrl;
            this.user = userPersister;
        },
        add: function (articleId, content, parentCommentId) {
            var url = this.serviceUrl + "?sessionId=" + this.user._getSessionId();
            var data = {
                content: content,
                articleId: articleId,
                parentCommentId: parentCommentId
            };

            return HttpRequester.postJson(url, data);
        },
        update: function (commentId, content) {
            var url = this.serviceUrl + "/" + commentId + "?sessionId=" + this.user._getSessionId();
            var data = {
                content: content
            };

            return HttpRequester.putJson(url, data);
        },
        remove: function (id) {
            var url = this.serviceUrl + "/" + id + "?sessionId=" + this.user._getSessionId();

            return HttpRequester.deleteJson(url);
        }
    });

    var VotePersister = Class.create({
        init: function (serviceUrl, userPersister) {
            this.serviceUrl = serviceUrl;
            this.user = userPersister;
        },
        add: function (articleId, value) {
            var url = this.serviceUrl + "?sessionId=" + this.user._getSessionId();
            var data = {
                articleId: articleId,
                value: value
            };

            return HttpRequester.postJson(url, data);
        },
        update: function (id, articleId, value) {
            var url = this.serviceUrl + "/" + id + "?sessionId=" + this.user._getSessionId();
            var data = {
                id: id,
                articleId: articleId,
                value: value
            };

            return HttpRequester.putJson(url, data);
        }
    });

    return {
        getDataPersister: function (serviceUrl) {
            return new DataPersister(serviceUrl);
        }
    }
}());